package lee.study.BinaryTree;

class Node {
    public char val;
    public Node left;//左子树
    public Node right;//右子树

    public Node(char val) {//构造方法
        this.val = val;
    }
}

public class BinaryTree {
    public Node buildTree() {
        Node A = new Node('A');
        Node B = new Node('B');
        Node C = new Node('C');
        Node D = new Node('D');
        Node E = new Node('E');
        Node F = new Node('F');
        Node G = new Node('G');
        Node H = new Node('H');
        A.left = B;
        A.right = C;
        B.left = D;
        B.right = E;
        E.right = H;
        C.left = F;
        C.right = G;
        return A;
    }

    void preOrderTraversal(Node root) {//前序遍历
        if (root == null) {
            return;
        }
        //如果根节点不为空，先打印根，然后遍历左，然后再遍历右
        System.out.print(root.val + " ");
        preOrderTraversal(root.left);
        preOrderTraversal(root.right);
    }

    void inOrderTraversal(Node root) {//中序遍历
        if (root == null) {
            return;
        }

        inOrderTraversal(root.left);
        System.out.print(root.val + " ");
        inOrderTraversal(root.right);
    }

    void postOrderTraversal(Node root) {//后序遍历
        if (root == null) {
            return;
        }

        postOrderTraversal(root.left);
        postOrderTraversal(root.right);
        System.out.print(root.val + " ");
    }
}
